package com.hjy.cabinet.ws.config;

import com.hjy.cabinet.ws.controller.CabinetController;
import com.hjy.cabinet.ws.controller.KAController;
import com.hjy.cabinet.ws.controller.WsTestController;
import com.hjy.cabinet.ws.domain.ws.WsAnnotation;
import com.hjy.cabinet.ws.domain.ws.WsClassData;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class AnnotationScanner {

    // 假设我们有一个类集合
    private static Map<String,WsClassData> classesResult = new HashMap<>();


    // 扫描所有类中的方法，寻找标有 @MyAnnotation 注解的方法
    public void scan() {
        Set<Class<?>> classes = getAllClasses();
        for (Class<?> clazz : classes) {
            // 获取类中的所有方法
             Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                // 检查方法是否标有 @MyAnnotation 注解
                if (method.isAnnotationPresent(WsAnnotation.class)) {
                    // 获取注解的值
                    WsAnnotation annotation = method.getAnnotation(WsAnnotation.class);
                    String value = annotation.value();
                    System.out.println("Found @MyAnnotation on method: " + method.getName() + ", value: " + value);
                    if (classesResult.get(value) != null) {
                        throw new RuntimeException( "当前ws请求名称已使用： "+value);
                    }
                    WsClassData wsClassData = new WsClassData();
                    wsClassData.setClazz(clazz);
                    wsClassData.setMethod(method);

                    classesResult.put(value, wsClassData);

                }
            }
        }
    }

    private Set<Class<?>> getAllClasses() {
        Set<Class<?>> allClasses = new HashSet<>();
        allClasses.add(WsTestController.class);
        allClasses.add(CabinetController.class);
        allClasses.add(KAController.class);
        return allClasses;
    }




    public static Map<String,WsClassData> getClassesResult() {
        return classesResult;
    }


//    private Set<Class<?>> getClasses() {
//        Set<Class<?>> annotatedClasses = new HashSet<>();
//        TypeAnnotationsScanner typeAnnotationsScanner = new TypeAnnotationsScanner();
//        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
//                .forPackages("com.hjy.cabinet.ws.mapper")
//                .addScanners(new TypeAnnotationsScanner(), new SubTypesScanner())
//                .filterInputsBy(new FilterBuilder().exclude("excluded.package.*")); // 排除特定包
//        typeAnnotationsScanner.setConfiguration(configurationBuilder);
//        // 配置 Reflections，添加必要的扫描器
//        Reflections reflections = new Reflections("com.hjy.cabinet.ws.mapper",
//                typeAnnotationsScanner    , new SubTypesScanner());
//
//        // 查找带有 WsAnnotation 注解的类
//        Set<Class<?>> classesWithAnnotation = reflections.getTypesAnnotatedWith(WsAnnotation.class);
//        annotatedClasses.addAll(classesWithAnnotation);
//
//        return annotatedClasses;
//    }
}
